#include "protocolParser.h"


protocolParser::protocolParser(const QString filePath, QObject* parent)
    :QObject(parent)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "无法打开文件:" << file.errorString();
        return;
    }

    QDomDocument doc;
    if (!doc.setContent(&file)) {
        qDebug() << "XML解析失败";
        file.close();
        return;
    }
    file.close();

    m_root = doc.documentElement();
}

protocolParser::~protocolParser()
{
}

QSharedPointer<SignalSystem::Link4A> protocolParser::getLink4A()
{
    QSharedPointer<SignalSystem::Link4A> link4a = QSharedPointer<SignalSystem::Link4A>::create();
    link4a->frequencies = getParseFrequency(m_root);
    link4a->masterslave = parseMasterSlave(m_root);
    return link4a;
}

QSharedPointer<SignalSystem::ConventSimulation> protocolParser::getConventSimulation()
{
    QSharedPointer<SignalSystem::ConventSimulation> conSim = QSharedPointer<SignalSystem::ConventSimulation>::create();
    conSim->modulationMode = parseModulationType(m_root);
    conSim->FMOffest = getFMOffset(m_root);
    return conSim;
}

QSharedPointer<SignalSystem::Link11> protocolParser::getLink11()
{
    QSharedPointer<SignalSystem::Link11> link11 = QSharedPointer<SignalSystem::Link11>::create();

    link11->frequencies = getParseFrequency(m_root);;
    link11->masterslave = parseMasterSlave(m_root);;
    link11->frameLength = parseFrameLength(m_root);
    link11->modulationMode = parseModulationType(m_root);
    return link11;
}

QSharedPointer<SignalSystem::Link16> protocolParser::getLink16()
{
    QSharedPointer<SignalSystem::Link16> link16 = QSharedPointer<SignalSystem::Link16>::create();
    link16->workMode = detectWorkMode(m_root);
    link16->businessType = checkServiceType(m_root);
    link16->roles = parseNetRoles(m_root);
    link16->frameFormatType = getFrameFormat(m_root);
    link16->pulseWidth = getPulseWidth(m_root);
    link16->carrierCenterOffset = getCarrierOffset(m_root);
    link16->encrypted = checkEncryption(m_root);
    link16->FreqPoints = analyzeFrequencyHops(m_root);
    link16->TimeSlotNumberMap = getSlotNumbers(m_root);

    return link16;
}

QSharedPointer<SignalSystem::TTNT> protocolParser::getTTNT()
{
    QSharedPointer<SignalSystem::TTNT> ttnt = QSharedPointer<SignalSystem::TTNT>::create();
    ttnt->frequencies = getParseFrequency(m_root);
    ttnt->rateMode = getRateMode(m_root);
    return ttnt;
}

QSharedPointer<SignalSystem::HAVEQUICK> protocolParser::getHAVEQUICK()
{
    QSharedPointer<SignalSystem::HAVEQUICK> haveQuick = QSharedPointer<SignalSystem::HAVEQUICK>::create();
    haveQuick->modelType = getModeType(m_root);
    haveQuick->jumpSpeedMode = getHoppingSpeedMode(m_root);
    haveQuick->encrypted = checkEncryption(m_root);
    haveQuick->frequencies = getParseFrequency(m_root);
    return haveQuick;
}

QSharedPointer<SignalSystem::SINCGARS> protocolParser::getSINCGARS()
{
    QSharedPointer<SignalSystem::SINCGARS> sincGARS = QSharedPointer<SignalSystem::SINCGARS>::create();
    sincGARS->fixedFrequencyHopps = getParseFrequency(m_root);
    sincGARS->frequencies = getParseFrequency(m_root);
    return sincGARS;
}

QSharedPointer<SignalSystem::EPLRS> protocolParser::getEPLRS()
{
    QSharedPointer<SignalSystem::EPLRS> EPLRS = QSharedPointer<SignalSystem::EPLRS>::create();
    EPLRS->waveMode = getWaveMode(m_root);
    return EPLRS;
}

QSharedPointer<SignalSystem::UV> protocolParser::getUV()
{
    QSharedPointer<SignalSystem::UV> uv = QSharedPointer < SignalSystem::UV > ::create();
    uv->modulationMode = parseModulationType(m_root);
    uv->jumpSpeed = getHoppingSpeed(m_root);
    return uv;
}

QSharedPointer<SignalSystem::radioStation171> protocolParser::getRadioStation171()
{
    QSharedPointer<SignalSystem::radioStation171> station171 = QSharedPointer < SignalSystem::radioStation171 > ::create();
    station171->frequencies = getParseFrequency(m_root);
    station171->jumpSpeed = getHoppingSpeed(m_root);
    station171->orificeSpeed = getOrificeSpeed(m_root);
    return station171;
}

QString protocolParser::getFMOffset(QDomElement root)
{
    QString result = "";
    QDomElement modu_FM = root.firstChildElement("ParamContainers")
        .firstChildElement("WaveFunction_Container")
        .firstChildElement("SignalModu_Container")
        .firstChildElement("Modu_FM");
    QDomNodeList modulationParams = modu_FM.elementsByTagName("LongLong-tag_sFMModulationParam");
    for (int i = 0; i < modulationParams.size(); ++i) {
        QDomElement mode = modulationParams.at(i).toElement();
        result = mode.firstChildElement("Value").attribute("FrequencyOffset");
        break;
    }
    return result;
}


QString protocolParser::getAntijamPolicyID(QDomElement root)
{
    QDomElement workModes = root.firstChildElement("WorkModes");
    QDomNodeList modeNodes = workModes.elementsByTagName("tag_sNetWorkModeFromUI");
    for (int i = 0; i < modeNodes.size(); ++i) {
        QDomElement mode = modeNodes.at(i).toElement();
        if (mode.attribute("IsDefault") == "1") {
            int policyIndex = mode.attribute("UsingAntijamPolicy").toInt();
            QDomNodeList policyIDs = mode.firstChildElement("AntijamPolicyIDs")
                .elementsByTagName("LongLong");
            if (policyIDs.size() > policyIndex) {
                return policyIDs.at(policyIndex).toElement().text();
            }
        }
    }
    return "";
}

double protocolParser::parseFrameLength(QDomElement root)
{
    QString pulseID = getPulseID(root);

    // 获取帧长
    QDomElement plusContainer = root.firstChildElement("ParamContainers")
        .firstChildElement("Pulse_Container")
        .firstChildElement("Pulses");
    QDomNodeList pulses = plusContainer.elementsByTagName("LongLong-tag_sPulseFromUI");

    double framLength = 0;
    for (int i = 0; i < pulses.size(); ++i) {
        QDomElement frame = pulses.at(i).toElement();
        if (frame.firstChildElement("Key").text() == pulseID) {
            framLength = frame.firstChildElement("Value")
                .firstChildElement("PulseWidth_ns")
                .attribute("Range_Min").toDouble();

            break;
        }
    }
    return framLength;
}

QString protocolParser::parseModulationType(QDomElement root)
{
    QString waveLinkID = getDefaultWaveLinkID(root);
    if (waveLinkID.isEmpty()) return "";

    // 获取调制方式参数
    QDomNodeList links = root.firstChildElement("ParamContainers")
        .firstChildElement("WaveLink_Container")
        .firstChildElement("WaveLinks")
        .elementsByTagName("LongLong-tag_sWaveLink");

    QString modulation;
    for (int i = 0; i < links.size(); ++i) {
        QDomElement link = links.at(i).toElement();
        if (link.firstChildElement("Key").text() == waveLinkID) {
            QDomNodeList functions = link.firstChildElement("Value")
                .firstChildElement("WaveFunctions")
                .elementsByTagName("tag_sFunctionConfig");
            for (int j = 0; j < functions.size(); ++j) {
                QDomElement func = functions.at(j).toElement();
                if (func.attribute("FunctionType") == "8") {
                    int subType = func.attribute("FunctionSubType").toInt();
                    modulation = m_modulationMap.value(subType, "未知");

                    break;
                }
            }
        }
    }
    return modulation;
}



QVector<quint64> protocolParser::getParseFrequency(const QDomElement& root)
{
    QVector<quint64> result;
    QString selectedPolicyID = getAntijamPolicyID(root);
    if (selectedPolicyID.isEmpty()) return {};


    // 3. 查找抗干扰策略
    QDomElement antijamContainer = root.firstChildElement("ParamContainers")
        .firstChildElement("AntijamPolicy_Container")
        .firstChildElement("AntijamPolicys");
    QDomNodeList policies = antijamContainer.elementsByTagName("LongLong-tag_sAntijamPolicy");

    QString dataBitFreqHoppingID;
    for (int i = 0; i < policies.size(); ++i) {
        QDomElement policy = policies.at(i).toElement();
        if (policy.firstChildElement("Key").text() == selectedPolicyID) {
            QDomElement config = policy.firstChildElement("Value")
                .firstChildElement("FreqPointsConfig")
                .firstChildElement("tag_sFreqPointsConfig");
            dataBitFreqHoppingID = config.attribute("DataBitFreqHoppingID");
            break;
        }
    }
    // 4. 获取频率点集ID
    QDomElement freqHoppings = root.firstChildElement("ParamContainers")
        .firstChildElement("FreqHopping_Container")
        .firstChildElement("FreqHoppings");
    QDomNodeList hoppingNodes = freqHoppings.elementsByTagName("LongLong-tag_sFreqHopping");

    QString freqPointsID;
    for (int i = 0; i < hoppingNodes.size(); ++i) {
        QDomElement hopping = hoppingNodes.at(i).toElement();
        if (hopping.firstChildElement("Key").text() == dataBitFreqHoppingID) {
            freqPointsID = hopping.firstChildElement("Value").attribute("FreqPointsID");
            break;
        }
    }
    // 5. 获取实际频率值
    QDomElement freqPointsContainer = root.firstChildElement("ParamContainers")
        .firstChildElement("FreqPoints_Container")
        .firstChildElement("FreqPoints");
    QDomNodeList freqPointSets = freqPointsContainer.elementsByTagName("LongLong-tag_sFreqPoints");

    for (int i = 0; i < freqPointSets.size(); ++i) {
        QDomElement set = freqPointSets.at(i).toElement();
        if (set.firstChildElement("Key").text() == freqPointsID) {
            QDomNodeList freqs = set.firstChildElement("Value")
                .firstChildElement("FreqPoints")
                .elementsByTagName("LongLong");

            for (int j = 0; j < freqs.size(); ++j) {
                result.push_back(freqs.at(j).toElement().text().toLongLong());
            }
            break;
        }
    }
    return result;
}

QVector<QSharedPointer<Link4AMS>> protocolParser::parseMasterSlave(const QDomElement& root)
{
    QDomElement targetInfos = root.firstChildElement("TargetInfos");
    QDomNodeList targets = targetInfos.elementsByTagName("tag_sTargetInfoInNet");
    QVector<QSharedPointer<Link4AMS>> vms;
    for (int i = 0; i < targets.size(); ++i) {
        QSharedPointer<Link4AMS> ms = QSharedPointer<Link4AMS>::create();
        QDomElement target = targets.at(i).toElement();
        QString targetID = target.attribute("TargetID");
        QString role = target.attribute("NetRole") == "0" ? "主站" : "从站";

        ms->TargetID = targetID;
        ms->role = role;

        vms.push_back(ms);
    }
    return vms;
}

QString protocolParser::detectWorkMode(QDomElement root)
{
    QDomNodeList modeNodes = root.firstChildElement("WorkModes")
        .elementsByTagName("tag_sNetWorkModeFromUI");

    int modeNumber = -1;
    for (int i = 0; i < modeNodes.count() && i < 3; ++i) {
        if (modeNodes.at(i).toElement().attribute("IsDefault") == "1") {
            modeNumber = (i == 0) ? 1 : (i == 1) ? 2 : 4;
            break;
        }
    }

    return QString::number(modeNumber);
}

QString protocolParser::checkServiceType(QDomElement root)
{
    QDomNodeList workGroups = root.firstChildElement("NetControllers")
        .firstChildElement("tag_sNetControllerFromUI")
        .firstChildElement("WorkGroups")
        .elementsByTagName("tag_sWorkGroupParamFromUI");

    bool hasDataTrans = false;
    bool hasVoice = false;

    for (int i = 0; i < workGroups.count(); ++i) {
        QString name = workGroups.at(i).toElement().firstChildElement("BaseInfo").attribute("Name");
        if (name.contains("数传")) hasDataTrans = true;
        if (name.contains("数字话音")) hasVoice = true;
    }

    if (hasDataTrans && hasVoice) {
        return "数话同传";
    }
    else if (hasDataTrans) {
        return "数传";
    }
    else if (hasVoice) {
        return "数字话音";
    }
    else {
        return "未识别业务类型";
    }

}

QVector<QSharedPointer<Link4AMS>> protocolParser::parseNetRoles(QDomElement root)
{
    QDomElement targetInfos = root.firstChildElement("TargetInfos");
    QDomNodeList targets = targetInfos.elementsByTagName("tag_sTargetInfoInNet");
    QVector<QSharedPointer<Link4AMS>> vms;
    for (int i = 0; i < targets.size(); ++i) {
        QSharedPointer<Link4AMS> ms = QSharedPointer<Link4AMS>::create();
        QDomElement target = targets.at(i).toElement();
        QString targetID = target.attribute("TargetID");
        QString role = target.attribute("NetRole") == "0" ? "NTR" : "JU";

        ms->TargetID = targetID;
        ms->role = role;

        vms.push_back(ms);
    }
    return vms;
}

QString protocolParser::getDefaultWaveLinkID(QDomElement root)
{
    QString selectedPolicyID = getAntijamPolicyID(root);
    if (selectedPolicyID.isEmpty()) return "";

    QDomNodeList policyList = root.firstChildElement("ParamContainers")
        .firstChildElement("AntijamPolicy_Container")
        .firstChildElement("AntijamPolicys")
        .elementsByTagName("LongLong-tag_sAntijamPolicy");

    for (int i = 0; i < policyList.count(); ++i) {
        QDomElement policy = policyList.at(i).toElement();
        if (policy.firstChildElement("Key").text() == selectedPolicyID) {
            return policy.firstChildElement("Value")
                .firstChildElement("WaveAndBitRate")
                .firstChildElement("tag_sWaveWithBitRate")
                .attribute("WaveLinkID");
        }
    }
    return "";
}

QString protocolParser::getFunctionParamID(QDomElement root)
{
    QString waveLinkID = getDefaultWaveLinkID(root);
    // 查找波形链路配置
    QDomNodeList waveLinks = root.firstChildElement("ParamContainers")
        .firstChildElement("WaveLink_Container")
        .firstChildElement("WaveLinks")
        .elementsByTagName("LongLong-tag_sWaveLink");

    QString frameConfigID;
    for (int i = 0; i < waveLinks.count(); ++i) {
        QDomElement link = waveLinks.at(i).toElement();
        if (link.firstChildElement("Key").text() == waveLinkID) {
            QDomNodeList functions = link.firstChildElement("Value")
                .firstChildElement("WaveFunctions")
                .elementsByTagName("tag_sFunctionConfig");

            for (int j = 0; j < functions.count(); ++j) {
                QDomElement func = functions.at(j).toElement();
                if (func.attribute("FunctionType") == "5") {
                    frameConfigID = func.attribute("FunctionParamID");
                    break;
                }
            }
            break;
        }
    }
    return frameConfigID;
}

QString protocolParser::getPulseID(QDomElement root)
{
    QString functionParamID = getFunctionParamID(root);

    // 获取pulseID
    QDomNodeList frameConfigs = root.firstChildElement("ParamContainers")
        .firstChildElement("FrameConfig_Container")
        .firstChildElement("Frames")
        .elementsByTagName("LongLong-tag_sFrameConfigFromUI");

    QString pulseID;
    for (int i = 0; i < frameConfigs.count(); ++i) {
        QDomElement link = frameConfigs.at(i).toElement();
        if (link.firstChildElement("Key").text() == functionParamID) {
            QDomNodeList fields = link.firstChildElement("Value")
                .firstChildElement("Fields")
                .elementsByTagName("tag_sFrameFieldFromUI");

            for (int j = 0; j < fields.count(); ++j) {
                QDomElement field = fields.at(j).toElement().firstChildElement("FieldType");
                if (field.text() == "6") {
                    pulseID = fields.at(j).toElement().firstChildElement("PulseID").text();
                    break;
                }
            }
            break;
        }
    }
    return pulseID;
}

QString protocolParser::getFrameFormat(QDomElement root)
{
    QString frameConfigID = getFunctionParamID(root);

    // 获取帧格式名称
    QDomNodeList frameConfigs = root.firstChildElement("ParamContainers")
        .firstChildElement("FrameConfig_Container")
        .firstChildElement("Frames")
        .elementsByTagName("LongLong-tag_sFrameConfigFromUI");

    for (int i = 0; i < frameConfigs.count(); ++i) {
        QDomElement config = frameConfigs.at(i).toElement();
        if (config.firstChildElement("Key").text() == frameConfigID) {

            return config.firstChildElement("Value").firstChildElement("BaseInfo").attribute("Name");
        }
    }
    return "";
}

double protocolParser::getPulseWidth(QDomElement root)
{
    QString pulseID = getPulseID(root);

    // 获取脉冲宽度
    QDomNodeList pulses = root.firstChildElement("ParamContainers")
        .firstChildElement("Pulse_Container")
        .firstChildElement("Pulses")
        .elementsByTagName("LongLong-tag_sPulseFromUI");

    for (int i = 0; i < pulses.count(); ++i) {
        QDomElement pulse = pulses.at(i).toElement();
        if (pulse.firstChildElement("Key").text() == pulseID) {
            double width = pulse.firstChildElement("Value")
                .firstChildElement("PulseWidth_ns")
                .attribute("Range_Min").toDouble();
            return width;
        }
    }
    return 0;
}

int protocolParser::getCarrierOffset(QDomElement root)
{
    int offset = root.attribute("FrequencyOffset").toInt();
    return offset;
}

bool protocolParser::checkEncryption(QDomElement root)
{
    QString waveLinkID = getDefaultWaveLinkID(root);
    QDomNodeList waveLinks = root.firstChildElement("ParamContainers")
        .firstChildElement("WaveLink_Container")
        .firstChildElement("WaveLinks")
        .elementsByTagName("LongLong-tag_sWaveLink");
    bool encrypted = false;
    for (int i = 0; i < waveLinks.count(); ++i) {
        QDomElement link = waveLinks.at(i).toElement();
        if (link.firstChildElement("Key").text() != waveLinkID) continue;
        QDomNodeList functions = link.firstChildElement("Value")
            .firstChildElement("WaveFunctions")
            .elementsByTagName("tag_sFunctionConfig");

        for (int j = 0; j < functions.count(); ++j) {
            QDomElement func = functions.at(j).toElement();
            QString funcType = func.attribute("FunctionType");
            if (funcType == "4" || funcType == "7") {
                encrypted = true;
                break;
            }
        }
        break;
    }
    return false;
}

QSharedPointer<FrequencyHops> protocolParser::analyzeFrequencyHops(QDomElement root)
{
    QString waveLinkID = getDefaultWaveLinkID(root);
    // 获取抗干扰策略
    QDomElement antijamPolicy;
    QDomNodeList policyList = root.firstChildElement("ParamContainers")
        .firstChildElement("AntijamPolicy_Container")
        .firstChildElement("AntijamPolicys")
        .elementsByTagName("LongLong-tag_sAntijamPolicy");

    for (int i = 0; i < policyList.count(); ++i) {
        QDomElement policy = policyList.at(i).toElement();
        if (policy.firstChildElement("Key").text() == getAntijamPolicyID(root)) {
            antijamPolicy = policy.firstChildElement("Value");
            break;
        }
    }
    if (antijamPolicy.isNull()) return nullptr;
    // 获取跳频配置ID
    QString sketchyID = antijamPolicy.firstChildElement("FreqPointsConfig").firstChildElement("tag_sFreqPointsConfig").attribute("SketchyFreqHoppingID");
    QString exactID = antijamPolicy.firstChildElement("FreqPointsConfig").firstChildElement("tag_sFreqPointsConfig").attribute("ExactFreqHoppingID");
    QString dataID = antijamPolicy.firstChildElement("FreqPointsConfig").firstChildElement("tag_sFreqPointsConfig").attribute("DataBitFreqHoppingID");
    // 获取频率点集ID
    auto getHoppingParam = [&](const QString& hopID) -> QString {
        QDomNodeList hops = root.firstChildElement("ParamContainers")
            .firstChildElement("FreqHopping_Container")
            .firstChildElement("FreqHoppings")
            .elementsByTagName("LongLong-tag_sFreqHopping");

        for (int i = 0; i < hops.count(); ++i) {
            QDomElement hop = hops.at(i).toElement();
            if (hop.firstChildElement("Key").text() == hopID) {
                return hop.firstChildElement("Value").attribute("FreqPointsID");
            }
        }
        return "";
        };

    auto getFreqInfo = [&](const QString& id) {
        QString pointsID = getHoppingParam(id);
        QVector<quint64> frequencies = getFreqPoints(root, pointsID);

        return frequencies;
        };

    QSharedPointer<FrequencyHops> result = QSharedPointer<FrequencyHops>::create();
    result->sketchyFrequencies = getFreqInfo(sketchyID);
    result->exactFrequencies = getFreqInfo(exactID);
    result->dataFrequencies = getFreqInfo(dataID);

    return result;
}

QVector<quint64> protocolParser::getFreqPoints(QDomElement root, const QString& freqPointsID)
{
    QVector<quint64> points;
    QDomNodeList freqPointsList = root.firstChildElement("ParamContainers")
        .firstChildElement("FreqPoints_Container")
        .firstChildElement("FreqPoints")
        .elementsByTagName("LongLong-tag_sFreqPoints");

    for (int i = 0; i < freqPointsList.count(); ++i) {
        QDomElement fp = freqPointsList.at(i).toElement();
        if (fp.firstChildElement("Key").text() != freqPointsID) continue;
        QDomNodeList frNodes = fp.firstChildElement("Value")
            .firstChildElement("FreqPoints")
            .elementsByTagName("LongLong");

        for (int j = 0; j < frNodes.count(); ++j) {
            points.push_back(frNodes.at(j).toElement().text().toDouble());
        }
    }
    return points;

}

QMap<QString, QStringList> protocolParser::getSlotNumbers(QDomElement root)
{
    QDomNodeList workGroupParams = root.firstChildElement("NetControllers")
        .firstChildElement("tag_sNetControllerFromUI")
        .firstChildElement("WorkGroups")
        .elementsByTagName("tag_sWorkGroupParamFromUI");

    QMap<QString, QStringList> slotMap;
    for (int i = 0; i < workGroupParams.count(); ++i) {
        QDomNodeList blocks = workGroupParams.at(i).toElement().firstChildElement("LogicSlotBlocks").elementsByTagName("tag_sLogicSlotBlock");

        for (int j = 0; j < blocks.size(); j++) {
            QDomElement block = blocks.at(j).toElement();
            QString targetID = block.attribute("TargetID");
            QDomNodeList slotBlockIDs = block.firstChildElement("MappedSlotBlockIDs").elementsByTagName("string");
            for (int k = 0; k < slotBlockIDs.size(); k++) {
                slotMap[targetID].append(slotBlockIDs.at(k).toElement().text());

            }
        }
    }
    return slotMap;

}

QString protocolParser::getRateMode(QDomElement root)
{
    QString selectedPolicyID = getAntijamPolicyID(root);

    QDomNodeList policies = root.firstChildElement("ParamContainers")
        .firstChildElement("AntijamPolicy_Container")
        .firstChildElement("AntijamPolicys")
        .elementsByTagName("LongLong-tag_sAntijamPolicy");

    QString rateMode;
    for (int i = 0; i < policies.count(); ++i) {
        QDomElement policy = policies.at(i).toElement();
        if (policy.firstChildElement("Key").text() == selectedPolicyID) {
            rateMode = policy.firstChildElement("Value").firstChildElement("BaseInfo").attribute("Note");
            break;
        }
    }
    return rateMode;
}

QString protocolParser::getModeType(QDomElement root)
{
    QDomNodeList modeNodes = root.firstChildElement("WorkModes")
        .elementsByTagName("tag_sNetWorkModeFromUI");

    QString modeType;
    for (int i = 0; i < modeNodes.count() && i < 3; ++i) {
        if (modeNodes.at(i).toElement().attribute("IsDefault") == "1") {
            modeType = (i == 0) ? "HQI" : (i == 1) ? "HQII" : "HQIIA";
            break;
        }
    }

    return modeType;
}

QString protocolParser::getHoppingSpeedMode(QDomElement root)
{
    QString pulseID = getPulseID(root);

    // 跳速模式
    QDomNodeList pulses = root.firstChildElement("ParamContainers")
        .firstChildElement("Pulse_Container")
        .firstChildElement("Pulses")
        .elementsByTagName("LongLong-tag_sPulseFromUI");

    QString rangeNum;
    for (int i = 0; i < pulses.count(); ++i) {
        QDomElement link = pulses.at(i).toElement();
        if (link.firstChildElement("Key").text() == pulseID) {
            QDomElement fields = link.firstChildElement("Value")
                .firstChildElement("PulseWidth_ns");

            rangeNum = fields.attribute("IsRange");
            break;
        }
    }
    return rangeNum;
}

QString protocolParser::getWaveMode(QDomElement root)
{
    QDomElement workModes = root.firstChildElement("WorkModes");
    QDomNodeList modeNodes = workModes.elementsByTagName("tag_sNetWorkModeFromUI");
    for (int i = 0; i < modeNodes.size(); ++i) {
        QDomElement mode = modeNodes.at(i).toElement();
        if (mode.attribute("IsDefault") == "1") {
            return mode.attribute("UsingAntijamPolicy");
        }
    }
    return "";
}

double protocolParser::getHoppingSpeed(QDomElement root)
{
    QString pulseID = getPulseID(root);

    QDomNodeList pulses = root.firstChildElement("ParamContainers")
        .firstChildElement("Pulse_Container")
        .firstChildElement("Pulses")
        .elementsByTagName("LongLong-tag_sPulseFromUI");

    double pulseWidth = 0;
    double pulseInterval = 0;
    for (int i = 0; i < pulses.count(); ++i) {
        QDomElement link = pulses.at(i).toElement();
        if (link.firstChildElement("Key").text() == pulseID) {
            QDomElement PulseWidth_ns = link.firstChildElement("Value")
                .firstChildElement("PulseWidth_ns");
            QDomElement PulseInterval_ns = link.firstChildElement("Value")
                .firstChildElement("PulseInterval_ns");

            pulseWidth = PulseWidth_ns.attribute("Range_Min").toDouble();
            pulseInterval = PulseInterval_ns.attribute("Range_Min").toDouble();
            break;
        }
    }
    double range = pulseWidth + pulseInterval;
    if (range != 0) {
        return 1.0 / range;
    }
    return 0;
}

QString protocolParser::getOrificeSpeed(QDomElement root)
{
    QString selectedPolicyID = getAntijamPolicyID(root);

    QDomNodeList policyList = root.firstChildElement("ParamContainers")
        .firstChildElement("AntijamPolicy_Container")
        .firstChildElement("AntijamPolicys")
        .elementsByTagName("LongLong-tag_sAntijamPolicy");

    for (int i = 0; i < policyList.count(); ++i) {
        QDomElement policy = policyList.at(i).toElement();
        if (policy.firstChildElement("Key").text() == selectedPolicyID) {
            return policy.firstChildElement("Value")
                .firstChildElement("WaveAndBitRate")
                .firstChildElement("tag_sWaveWithBitRate")
                .attribute("BitRate");
        }
    }
    return "";
}